package inet.db.tennis;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import inet.db.pool.*;
import inet.util.Logger;

public class TennisSeasonDAO {

	private Logger logger = null;
	private DBPoolX poolX = null;
	public TennisSeasonDAO(){
		logger = new Logger(this.getClass().getName());
		try{
			poolX = DBPoolX.getInstance(DBPoolXName.SERVICE_BONGDA);
		}catch(Exception exception){
			logger.log(exception.toString()); 
		}
	}
	
	/**
	 * function get a TennisSeason by id
	 * @param id
	 * @return
	 */
    public TennisSeason getRow(BigDecimal id) {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        String strSQL = null;
        TennisSeason season = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "select ID, CHAMPION_CODE, SEASON_NAME, LAST_UPDATED from TENNIS_SEASON where ID = ? ";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setBigDecimal(1, id);
            rs = preStmt.executeQuery();
            if (rs.next()) {
            	season = new TennisSeason();
            	season.setId(rs.getBigDecimal(1));
            	season.setChampionCode(rs.getString(2));
            	season.setSeasonName(rs.getString(3));
            	season.setLastUpdated(rs.getTimestamp(4));
            }
        }
        catch (SQLException e) {
            logger.error("getRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt, rs);
        }
        return season;
    }
    
    /**
     * fucntion get all season
     * @return Vector<TennisSeason> 
     * TennisSeason (ID, CHAMPION_CODE, SEASON_NAME, LAST_UPDATED)
     */
    public Vector<TennisSeason> findAll() {
    	Connection conn = null;
    	PreparedStatement preStmt = null;
    	ResultSet rs = null;
    	String strSQL = null;
    	TennisSeason season = null;
    	Vector <TennisSeason> vSeason = null;
    	
    	try {
    		conn = poolX.getConnection();
    		strSQL =
    			"select ID, CHAMPION_CODE, SEASON_NAME, LAST_UPDATED from TENNIS_SEASON ";
    		preStmt = conn.prepareStatement(strSQL);
    		rs = preStmt.executeQuery();
    		vSeason = new Vector<TennisSeason>();
    		while (rs.next()) {
    			season = new TennisSeason();
    			season.setId(rs.getBigDecimal(1));
            	season.setChampionCode(rs.getString(2));
            	season.setSeasonName(rs.getString(3));
            	season.setLastUpdated(rs.getTimestamp(4));
    			
    			vSeason.addElement(season);
    		}
    	}
    	catch (SQLException e) {
    		logger.error("findAll: Error executing SQL " + strSQL + ">>>" + e.toString());
    	} finally {
    		poolX.releaseConnection(conn, preStmt, rs);
    	}
    	return vSeason;
    }
    
    /**
     * function get season of champion
     * @param championCode
     * @return Vector<TennisSeason>
     */
    public Vector<TennisSeason> findAll(String championCode) {
    	Connection conn = null;
    	PreparedStatement preStmt = null;
    	ResultSet rs = null;
    	String strSQL = null;
    	TennisSeason season = null;
    	Vector <TennisSeason> vSeason = null;
    	
    	try {
    		conn = poolX.getConnection();
    		strSQL =
    			"select ID, CHAMPION_CODE, SEASON_NAME, LAST_UPDATED from TENNIS_SEASON where CHAMPION_CODE = ? ";
    		preStmt = conn.prepareStatement(strSQL);
    		preStmt.setString(1, championCode);
    		
    		rs = preStmt.executeQuery();
    		vSeason = new Vector<TennisSeason>();
    		while (rs.next()) {
    			season = new TennisSeason();
    			season.setId(rs.getBigDecimal(1));
    			season.setChampionCode(rs.getString(2));
    			season.setSeasonName(rs.getString(3));
    			season.setLastUpdated(rs.getTimestamp(4));
    			
    			vSeason.addElement(season);
    		}
    	}
    	catch (SQLException e) {
    		logger.error("findAll: Error executing SQL " + strSQL + ">>>" + e.toString());
    	} finally {
    		poolX.releaseConnection(conn, preStmt, rs);
    	}
    	return vSeason;
    }
	
    /**
     * function insert a TennisSeason
     * @param name
     * @return true if insert success else return false.
     */
	public boolean insertRow(String championCode, String seasonName){
		Connection conn = null;
        PreparedStatement preStmt = null;
        boolean result = false;
        String strSQL = null;
        try {
            conn = poolX.getConnection();

        	strSQL =" insert into TENNIS_SEASON( ID, CHAMPION_CODE, SEASON_NAME, LAST_UPDATED ) " +
    				" values(TENNIS_SEASON_SEQ.nextval, ?, ?, sysdate) ";
	        preStmt = conn.prepareStatement(strSQL);
	        preStmt.setString(1, championCode);  
	        preStmt.setString(2, seasonName);  
	        
	        if (preStmt.executeUpdate()>=1) {
	        	result = true;
	        }
        } catch (Exception ex) {
        	logger.error("insertRow: Error executing SQL " + strSQL + ">>>" + ex.toString());            
        } finally {
        	poolX.releaseConnection(conn, preStmt);
       }
       return result;
	}
	
	/**
	 * function update TennisSeason
	 * @param id
	 * @param name
	 * @return true if update success else return false.
	 */
	public boolean updateRow(BigDecimal id, String seasonName){
		Connection conn = null;
        PreparedStatement preStmt = null;
        boolean result = false;
        String strSQL = null;
        try {
        	conn = poolX.getConnection();
        	strSQL =" update TENNIS_SEASON set SEASON_NAME = ? " +
        			" where ID = ?";
        	preStmt = conn.prepareStatement(strSQL);
        	preStmt.setString(1, seasonName);
        	preStmt.setBigDecimal(2, id);
        	
        	if(preStmt.executeUpdate() > 0){
        		result = true;
        	}
        } catch (Exception ex) {
        	logger.error("updateRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
        } finally {
        	poolX.releaseConnection(conn, preStmt);
       }
        return result;
	}	

	/**
	 * function delete a TennisSeason
	 * @param id
	 * @return true if update success else return false.
	 */
	public boolean deleteRow(BigDecimal id){
		Connection conn = null;
		PreparedStatement preStmt = null;
		boolean result = false;
		String strSQL = null;
		try {
			conn = poolX.getConnection();
			strSQL =" delete from TENNIS_SEASON where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			if(preStmt.executeUpdate() > 0){
				result = true;
			}
		} catch (Exception ex) {
			logger.error("deleteRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt);
		}
		return result;
	}
}
